class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int ret = 0, pos = nums.size() - 1; // 固定的数
        for(int i = pos; i >= 2; --i) // 因为至少三个数，所以大于等于2
        {
            int left = 0, right = i - 1;
            while(left < right)
            {
                if(nums[left] + nums[right] > nums[i])
                {
                    ret += (right - left);
                    --right;
                }
                else
                {
                    ++left;
                }
            }
        }
        return ret;
    }
};